﻿Imports System.Data

Partial Class Interfaz_frmBusClientes
    Inherits System.Web.UI.Page
    Dim CL As New ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet
    Dim data As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CL.visibilidadesFormsInternos("Busqueda", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim Array As ArrayList
        Dim i As Integer

        Array = CL.ObtenerProveedor()


        If Not Page.IsPostBack Then
            Me.DropRegion.Items.Add("I")
            Me.DropRegion.Items.Add("II")
            Me.DropRegion.Items.Add("III")
            Me.DropRegion.Items.Add("IV")
            Me.DropRegion.Items.Add("V")
            Me.DropRegion.Items.Add("VI")
            Me.DropRegion.Items.Add("VII")
            Me.DropRegion.Items.Add("VIII")
            Me.DropRegion.Items.Add("IX")
            Me.DropRegion.Items.Add("X")
            Me.DropRegion.Items.Add("XI")
            Me.DropRegion.Items.Add("XII")

            For i = 0 To Array.Count - 1
                Me.DropGiro.Items.Add(DirectCast(Array.Item(i), Proveedor).Giro)
                Me.DropCiudad.Items.Add(DirectCast(Array.Item(i), Proveedor).Ciudad)
                Me.DropRut.Items.Add(DirectCast(Array.Item(i), Proveedor).Rut)
            Next
        End If

        'se guardan datos en secion para el positback de la pagina respecto al los datos del datagridview
        If Session("busquedaProoveedor") Is Nothing Then
            Session("busquedaProoveedor") = dtDatos
        Else
            dtDatos = DirectCast(Session("busquedaProoveedor"), DataTable)
        End If

        If Not Page.IsPostBack Then
            Try
                dtDatos.Columns.Add(New DataColumn("RUT"))
                dtDatos.Columns.Add(New DataColumn("RZNSOCIAL"))
                dtDatos.Columns.Add(New DataColumn("CIUDAD"))
                dtDatos.Columns.Add(New DataColumn("COMUNA"))
                dtDatos.Columns.Add(New DataColumn("REGION"))
                dtDatos.Columns.Add(New DataColumn("TELEFONO"))
                dtDatos.Columns.Add(New DataColumn("GIRO"))
                dtDatos.Columns.Add(New DataColumn("DIRECCION"))
                dtDatos.Columns.Add(New DataColumn("FAX"))
                dtDatos.Columns.Add(New DataColumn("EMAIL"))

                Me.GridProveedor.DataBind()
                Me.GridProveedor.DataSource = Me.dtDatos
            Catch
            End Try
        End If
        'Me.GridActividad.data.DataBind()
        Me.GridProveedor.DataSource = Me.dtDatos
    End Sub

#Region "Funciones"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub buscar()

        Dim ArrayProv As New ArrayList
        Dim i As Integer
        Dim query As String

        'Busqueda por rut
        If R1.Checked = True Then
            query = "select * from proveedor where rut ='" + DropRut.SelectedItem.Value + "'"
            ArrayProv = CL.ObtenerProveedor(query)
        Else
            'Busqueda por region
            If R2.Checked = True Then
                query = "select * from proveedor where region ='" + DropRegion.SelectedItem.Value + "'"
                ArrayProv = CL.ObtenerProveedor(query)
            Else
                'Busqueda por giro
                If R3.Checked = True Then
                    query = "select * from proveedor where giro ='" + DropGiro.SelectedItem.Value + "'"
                    ArrayProv = CL.ObtenerProveedor(query)
                Else
                    'Busqueda por ciudad
                    If R4.Checked = True Then
                        query = "select * from proveedor where ciudad ='" + DropCiudad.SelectedItem.Value + "'"
                        ArrayProv = CL.ObtenerProveedor(query)
                    Else
                        If R5.Checked = True Then
                            query = "select * from proveedor where rznsocial ='" + Me.TxTRznSocial.Text + "'"
                            ArrayProv = CL.ObtenerProveedor(query)
                        Else
                            query = "select * from proveedor"
                            ArrayProv = CL.ObtenerProveedor(query)
                        End If
                    End If

                End If
            End If
        End If

        Try

            For i = 0 To ArrayProv.Count - 1

                Dim drDatos As DataRow = dtDatos.NewRow
                drDatos.Item("RUT") = DirectCast(ArrayProv.Item(i), Proveedor).Rut
                drDatos.Item("RZNSOCIAL") = DirectCast(ArrayProv.Item(i), Proveedor).Rznsocial
                drDatos.Item("CIUDAD") = DirectCast(ArrayProv.Item(i), Proveedor).Ciudad
                drDatos.Item("COMUNA") = DirectCast(ArrayProv.Item(i), Proveedor).Comuna
                drDatos.Item("REGION") = DirectCast(ArrayProv.Item(i), Proveedor).Region
                drDatos.Item("TELEFONO") = DirectCast(ArrayProv.Item(i), Proveedor).Telefono
                drDatos.Item("GIRO") = DirectCast(ArrayProv.Item(i), Proveedor).Giro
                drDatos.Item("DIRECCION") = DirectCast(ArrayProv.Item(i), Proveedor).Direccion
                drDatos.Item("FAX") = DirectCast(ArrayProv.Item(i), Proveedor).Fax
                drDatos.Item("EMAIL") = DirectCast(ArrayProv.Item(i), Proveedor).Email

                dtDatos.Rows.Add(drDatos)
            Next
            Me.GridProveedor.DataSource = dtDatos
            Me.GridProveedor.DataBind()
            Me.GridProveedor.ForeColor = Drawing.Color.Blue

        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try
        clean()
    End Sub

    Private Sub clean()
        Me.R1.Checked = False
        Me.R2.Checked = False
        Me.R3.Checked = False
        Me.R4.Checked = False
        Me.R5.Checked = False

    End Sub

    Private Sub vaciaGrid()
        data = DirectCast(Session("busquedaProoveedor"), DataTable)
        If Not Data Is Nothing Then
            Data.Clear()
            Session("busquedaProoveedor") = data
        End If
    End Sub

    Private Sub limpiar()
        TxTRznSocial.Text = ""
        clean()
    End Sub

#End Region

#Region "Botones"

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.vaciaGrid()
        buscar()
        limpiar()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        limpiar()
    End Sub
#End Region
   
End Class
